/**
 * 倒计时工具
 * 在退出页面时需要手动结束
 */
var clock = {
  state: "STOPED", // STOPED 停止状态 , RUNNING 执行中
  time: 0,
  interval: {},
  createClock: function(time = 60, onTimeChange, onFinish) {
    if (this.state == "STOPED") {
      var self = this;
      self.time = time;
      onTimeChange(self.time)
      self.interval = setInterval(function() {
        self.time -= 1;
        if (self.time <= 0) {
          clearInterval(self.interval)
          self.state = "STOPED"
          onFinish()
        } else {
          onTimeChange(self.time)
          self.state = "RUNNING"
        }
      }, 1000)
    }
  },
  endClock: function() {
    clearInterval(this.interval)
    this.state = "STOPED"
  }
}

module.exports = {
  clock: clock
}